1006C - Three Parts of the Array - CodeForces Solution


binary search data structures two pointers *1200

Please click on ads to support us..

Python Code:

input()
elementos = list(map(int, input().split()))

maior_soma = 0
soma_esquerda = 0
soma_direita = 0

esquerda = 0
direita = len(elementos) - 1

controle_incremento = 0

while esquerda < direita:

    if controle_incremento == 0:
        soma_esquerda += elementos[esquerda]
        soma_direita += elementos[direita]
    elif controle_incremento == 1:
        soma_esquerda += elementos[esquerda]
    else:
        soma_direita += elementos[direita]

    if soma_esquerda < soma_direita:
        esquerda += 1
        controle_incremento = 1
    elif soma_esquerda > soma_direita:
        direita -= 1
        controle_incremento = 2
    else:
        maior_soma = soma_esquerda
        esquerda += 1
        direita -= 1
        controle_incremento = 0

print(maior_soma)

 	 				    			  	 	 						 				

C++ Code:

#include<iostream>
using namespace std;
int main(){
    long long int n;
    // cout<<"check21";
    cin>>n;
    long long int arr[n];
    // vector<int>arr;
    // int arr[7]={1,2,3,4,5,6,7};
    for(long long int i=0;i<n;i++){
        cin>>arr[i];
    }
    
    long long int s1=arr[0];
    long long int s2=arr[n-1];
    long long int ans=0;
    long long int i=0;
    long long int j=n-1;
    // cout<<s1<<s2<<ans<<i<<j;

    while(i<j){
        if(s1==s2){
            ans=s1;
            // cout<<ans<<endl;
            i++;
            s1=s1+arr[i];
            j--;
            s2=s2+arr[j];
        }
        else if(s1<s2){
            i++;
            s1=s1+arr[i];
            // cout<<s1<<endl;
        }
        else if(s1>s2){
            j--;
            s2=s2+arr[j];
            // cout<<s2<<endl;
        }
    }
    cout<<ans;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves